package chapter21;
/*编程题21.1*/
public class MyGenericStack<E> {

	private int size = 0;
	private E[] list = (E[]) new Object[size];
	
	public int getSize(){
		return size;
	}
	
	public E peek(){
		/*调用此方法时，在小于size时，为什么会报数组越界的警告呢？*/
		//return list[getSize() - 1];
		return pop();
	}
	
	public void push(E o){
		E[] tmpList = (E[]) new Object[size + 1];
		if (size > 0){
			for (int i = 0; i < size; i++)
				tmpList[i] = list[i];
		}
		size += 1;
		tmpList[size - 1] = o;
		list = tmpList;
	}
	
	public E pop(){
		/*调用此方法时，在小于size时，为什么会报数组越界的警告呢？*/
		E[] tmpList = (E[]) new Object[size - 1];
		if (size > 0){
			for (int i = 0; i < size - 1; i++)
				tmpList[i] = list[i];
		}
		E result = list[size - 1];
		size -= 1;
		list = tmpList;
		return result;
		
	}
	
	public boolean isEmpty(){
		if (size == 0)
			return true;
		else
			return false;
	}
	
}
